package com.quectel.cms.controller.datav;

import cn.hutool.core.date.DateUtil;
import com.quectel.cms.constants.ScreenConstants;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.datav.ChargingCountResp;
import com.quectel.cms.vo.datav.HouseholdCarStatisticsVo;
import com.quectel.cms.vo.datav.PersonCarInOutTrendVo;
import com.quectel.constant.core.household.HouseholdCarConstants;
import com.quectel.core.module.car.service.mongo.MongoCarAccessRecordService;
import com.quectel.core.module.entranceguard.service.mongo.MongoPersonAccessRecordService;
import com.quectel.core.module.household.dto.datav.PersonInOutTrendDto;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import static com.quectel.constant.global.StatisticsConstants.*;

/**
 * @author yubobo
 * @email bobo.yu@quectel.com
 * @date 2022/1/10 下午2:17
 */
@RestController
@RequestMapping("datav/travel")
@Tag(name = "DataVTravelController", description = "大屏-出行管理相关接口")
public class DataVTravelController extends BaseController {

    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private MongoCarAccessRecordService carRecordService;
    @DubboReference
    private MongoPersonAccessRecordService personAccessRecordService;
    @DubboReference
    private VillageService villageService;

    /**
     * 充电桩未对接，模拟接口
     * @param type
     * @param villageId
     * @return
     */
    @GetMapping("chargingUseTrend")
    @Operation(summary = "充电桩使用统计")
    public Response<ChargingCountResp>chargingUseTrend(
            @Parameter(description = "日：DAY 周：WEEK 月：MONTH") @RequestParam String type,
            @Parameter(description = "小区id") @RequestParam Long villageId
    ){

        // 未接入真实设备，模拟统计接口
        VillageDto villageDto = villageService.selectCacheById(villageId);
        int total = 0;
        int[] dayArr = new int[31];
        int[] hourArr = new int[24];
        if (villageDto != null && "xkwc".equals(villageDto.getCode())) {
            total = 46;
            dayArr = new int[]{31,18,27,66,75,14,4,14,4,62,61,10,50,17,34,76,62,52,89,41,37,71,16,39,6,82,23,22,29,27,18};
            hourArr = new int[]{10,5,22,43,1,1,26,33,35,35,24,19,7,27,21,12,31,32,1,23,41,17,8,5};
        } else if (villageDto != null && "glxy".equals(villageDto.getCode())) {
            total = 20;
            dayArr = new int[]{28,16,8,28,22,13,34,16,4,37,19,1,18,11,11,24,36,8,29,8,23,32,0,11,35,9,17,28,30,11,21};
            hourArr = new int[]{1,4,8,18,17,14,7,7,10,5,8,14,4,8,15,1,12,5,1,5,4,11,3,5};
        }

        final Date currentDate = new Date();
        String[] xData = null;
        int[] yData = new int[]{};
        switch (type) {
            case TIME_RANGE_BY_DAY:
                xData = DateUtils.timeSlotOfHour(DateUtil.beginOfDay(currentDate)
                        , currentDate, DateUtils.FormatType.TIME_WITHOUT_SECONDS, 1);
                yData = Arrays.copyOfRange(hourArr, 0, xData.length);
                break;
            case TIME_RANGE_BY_WEEK:
                xData = DateUtils.timeSlotOfDay(DateUtil.beginOfWeek(currentDate)
                        , currentDate, DateUtils.FormatType.SIMPLE, 1);
                List<Integer> yDataList = new ArrayList<>();
                for (int i = 0; i < xData.length; i++) {
                    Date parse = DateUtils.parse(xData[i], DateUtils.FormatType.SIMPLE);
                    String cv = DateUtil.dayOfWeekEnum(parse).toChinese();
                    xData[i] = cv;
                    yDataList.add(dayArr[DateUtil.dayOfMonth(parse) - 1]);
                }
                yData = yDataList.stream().mapToInt(Integer::intValue).toArray();
                break;
            case TIME_RANGE_BY_MONTH:
                xData = DateUtils.timeSlotOfDay(DateUtil.beginOfMonth(currentDate)
                        , currentDate, DateUtils.FormatType.SIMPLE, 1);
                yData = Arrays.copyOfRange(dayArr, 0, xData.length);
                break;
            default:
                xData = new String[]{};
        }

        // TODO 根据小区展示总数，模拟一些年月日使用次数

        ChargingCountResp countResp = new ChargingCountResp();
        countResp.setTotal(total);
        countResp.setFaultCount(0);
        countResp.setXData(xData);
        countResp.setYData(new ChargingCountResp.DataInfo[]{
                new ChargingCountResp.DataInfo("充电次数", Arrays.stream(yData).boxed().toArray(Integer[]::new)),
        });
        return Response.<ChargingCountResp>ok().wrap(countResp);
    }

    /**
     * 查询本日
     * 进入小区
     * 离开小区
     * 访客人员
     *
     * @param villageId
     * @return
     */
    @GetMapping("countHouseholdStatistics")
    @Operation(summary = "人员进出统计")
    public Response<HouseholdCarStatisticsVo> countHousehold(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId,
            HttpServletRequest httpServletRequest
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);
        params.put("villageId", villageId);
        String startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        params.put("startTime", startTime + " 00:00:00");
        params.put("endTime", startTime + " 23:59:59");

        HouseholdCarStatisticsVo vo = new HouseholdCarStatisticsVo();

        params.put("outOrIn", HouseholdCarConstants.IN);
        vo.setInCount(personAccessRecordService.countTotal(params));

        params.put("outOrIn", HouseholdCarConstants.OUT);
        vo.setOutCount(personAccessRecordService.countTotal(params));

        // 住户访客=2
        params.put("personType", HouseholdCarConstants.CAR_TYPE_VISITOR);
        int visitorCount = personAccessRecordService.countTotal(params);
        vo.setVisitorCount(visitorCount);

        return Response.<HouseholdCarStatisticsVo>ok().wrap(vo);
    }

    /**
     * 查询本日
     *
     * @param villageId
     * @return
     */
    @GetMapping("countCarStatistics")
    @Operation(summary = "车辆进出统计")
    public Response<HouseholdCarStatisticsVo> countCarStatistics(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId,
            HttpServletRequest httpServletRequest
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        params.put("villageId", villageId);
        super.fillAuthorityData(params);

        String startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        params.put("startTime", startTime + " 00:00:00");
        params.put("endTime", startTime + " 23:59:59");

        HouseholdCarStatisticsVo vo = new HouseholdCarStatisticsVo();

        params.put("outOrIn", HouseholdCarConstants.IN);
        vo.setInCount(carRecordService.countTotal(params));

        params.put("outOrIn", HouseholdCarConstants.OUT);
        vo.setOutCount(carRecordService.countTotal(params));

        // 访客车辆=3
        params.put("carType", HouseholdCarConstants.CAR_TYPE_VISITOR);
        vo.setVisitorCount(carRecordService.countTotal(params));

        return Response.<HouseholdCarStatisticsVo>ok().wrap(vo);
    }


    /**
     * 人员进出趋势 1本日 2本周 3本月
     * 1进 2出
     */
    @GetMapping("personInOutTrend")
    @Operation(summary = "人员进出趋势折线图 1本日 2本周 3本月")
    public Response<Object> personInOutTrend(
            @Parameter(description = "时间区间类型 1本日 2本周 3本月 必填") @RequestParam(required = false) String scope,
            @Parameter(description = "小区id 非必填") @RequestParam(required = false) Long villageId) {
        if (StringUtils.isEmpty(scope)) {
            return Response.error("缺少查询范围参数或查询范围参数错误");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("scope", scope);
        params.put("villageId", villageId);
        super.fillAuthorityData(params);

        List<PersonCarInOutTrendVo> voList = new ArrayList<>();
        PersonCarInOutTrendVo resultVo = new PersonCarInOutTrendVo();

        // 本日
        if (ScreenConstants.STATISTICS_DAY.equals(scope)) {
            String startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            params.put("startTime", startTime + " 00:00:00");
            params.put("endTime", startTime + " 23:59:59");
            params.put("outOrIn", HouseholdCarConstants.IN);
            List<PersonInOutTrendDto> inTrendDtoList = personAccessRecordService.groupHours(params);
            for (int i = 0; i < inTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = inTrendDtoList.get(i);
                String time = personInOutTrendDto.getTime();
                int count = personInOutTrendDto.getCount();
                PersonCarInOutTrendVo person = new PersonCarInOutTrendVo();
                person.setTime(time);
                person.setInCount(count);
                voList.add(person);
            }

            List<String> hoursList = hoursList();
            for (int i = hoursList.size() - 1; i >= 0; i--) {
                String hourStr = hoursList.get(i);
                for (int k = 0; k < voList.size(); k++) {
                    PersonCarInOutTrendVo dto = voList.get(k);
                    if (hourStr.equals(dto.getTime())) {
                        hoursList.remove(hourStr);
                    }
                }
            }
            for (int i = 0; i < hoursList.size(); i++) {
                PersonCarInOutTrendVo nr = new PersonCarInOutTrendVo();
                nr.setTime(hoursList.get(i));
                nr.setOutCount(0);
                voList.add(nr);
            }

            params.put("outOrIn", HouseholdCarConstants.OUT);
            List<PersonInOutTrendDto> outTrendDtoList = personAccessRecordService.groupHours(params);
            for (int i = 0; i < outTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = outTrendDtoList.get(i);
                String t1 = personInOutTrendDto.getTime();
                for (int j = 0; j < voList.size(); j++) {
                    PersonCarInOutTrendVo person = voList.get(j);
                    String t2 = person.getTime();
                    if (t2.equals(t1)) {
                        person.setOutCount(personInOutTrendDto.getCount());
                    }
                }
            }
            List<PersonCarInOutTrendVo> collect =
                    voList.stream().sorted(Comparator.comparing(PersonCarInOutTrendVo::getTime)).collect(Collectors.toList());
            String[] ids = collect.stream().map(PersonCarInOutTrendVo::getTime).toArray(String[]::new);
            Integer[] ins = collect.stream().map(PersonCarInOutTrendVo::getInCount).toArray(Integer[]::new);
            Integer[] outs = collect.stream().map(PersonCarInOutTrendVo::getOutCount).toArray(Integer[]::new);

            resultVo.setTransverseAxis(ids);
            resultVo.setInCountAxis(ins);
            resultVo.setOutCountAxis(outs);

        }

        // 本周
        if (ScreenConstants.STATISTICS_WEEK.equals(scope)) {
            String[] thisWeeks = DateUtils.timeSlotOfDay(DateUtil.beginOfWeek(new Date()), new Date(), DateUtils.FormatType.DAY, 1);
            params.put("startTime", DateUtil.format(DateUtil.beginOfWeek(new Date()), "yyyy-MM-dd") + " 00:00:00");
            params.put("endTime", DateUtil.format(DateUtil.endOfWeek(new Date()), "yyyy-MM-dd") + " 23:59:59");
            params.put("outOrIn", HouseholdCarConstants.IN);

            for (int i = thisWeeks.length - 1; i >= 0; i--) {
                PersonCarInOutTrendVo record = new PersonCarInOutTrendVo();
                record.setTime(thisWeeks[i]);
                record.setInCount(0);
                record.setOutCount(0);
                voList.add(record);
            }
            List<PersonInOutTrendDto> inTrendDtoList = personAccessRecordService.groupDays(params);

            for (int j = 0; j < inTrendDtoList.size(); j++) {
                PersonInOutTrendDto personInOutTrendDto = inTrendDtoList.get(j);
                String recordTime = personInOutTrendDto.getTime();
                for (int i = 0; i < voList.size(); i++) {
                    if (voList.get(i).getTime().equals(recordTime)) {
                        voList.get(i).setInCount(personInOutTrendDto.getCount());
                    }
                }
            }


            params.put("outOrIn", HouseholdCarConstants.OUT);
            List<PersonInOutTrendDto> outTrendDtoList = personAccessRecordService.groupDays(params);
            for (int i = 0; i < outTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = outTrendDtoList.get(i);
                String recordTime = personInOutTrendDto.getTime();
                for (int j = 0; j < voList.size(); j++) {
                    if (voList.get(j).getTime().equals(recordTime)) {
                        voList.get(j).setOutCount(personInOutTrendDto.getCount());
                    }
                }
            }

            List<PersonCarInOutTrendVo> collect =
                    voList.stream().sorted(Comparator.comparing(PersonCarInOutTrendVo::getTime)).collect(Collectors.toList());
            String[] ids = collect.stream().map(PersonCarInOutTrendVo::getTime).toArray(String[]::new);
            Integer[] ins = collect.stream().map(PersonCarInOutTrendVo::getInCount).toArray(Integer[]::new);
            Integer[] outs = collect.stream().map(PersonCarInOutTrendVo::getOutCount).toArray(Integer[]::new);

            Calendar cal = Calendar.getInstance();
            String yearStr = String.valueOf(cal.get(Calendar.YEAR));
            for (int i = 0; i < ids.length; i++) {
                String format = yearStr + "-" + ids[i];
                String string = DateUtils.dateToWeek(DateUtil.parseDate(format));
                ids[i] = string;
            }
            resultVo.setTransverseAxis(ids);
            resultVo.setInCountAxis(ins);
            resultVo.setOutCountAxis(outs);

        }

        // 本月
        if (ScreenConstants.STATISTICS_MONTH.equals(scope)) {
            String[] thisMonthDays = DateUtils.timeSlotOfDay(DateUtil.beginOfMonth(new Date()), new Date(), DateUtils.FormatType.DAY, 1);
            params.put("startTime", DateUtil.format(DateUtil.beginOfMonth(new Date()), "yyyy-MM-dd") + " 00:00:00");
            params.put("endTime", DateUtil.format(DateUtil.endOfMonth(new Date()), "yyyy-MM-dd") + " 23:59:59");
            params.put("outOrIn", HouseholdCarConstants.IN);


            for (int i = thisMonthDays.length - 1; i >= 0; i--) {
                PersonCarInOutTrendVo record = new PersonCarInOutTrendVo();
                record.setTime(thisMonthDays[i]);
                record.setInCount(0);
                record.setOutCount(0);
                voList.add(record);
            }

            List<PersonInOutTrendDto> inTrendDtoList = personAccessRecordService.groupDays(params);

            for (int j = 0; j < inTrendDtoList.size(); j++) {
                PersonInOutTrendDto personInOutTrendDto = inTrendDtoList.get(j);
                String recordTime = personInOutTrendDto.getTime();
                for (int i = 0; i < voList.size(); i++) {
                    if (voList.get(i).getTime().equals(recordTime)) {
                        voList.get(i).setInCount(personInOutTrendDto.getCount());
                    }
                }
            }

            params.put("outOrIn", HouseholdCarConstants.OUT);
            List<PersonInOutTrendDto> outTrendDtoList = personAccessRecordService.groupDays(params);
            for (int i = 0; i < outTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = outTrendDtoList.get(i);
                String recordTime = personInOutTrendDto.getTime();
                for (int j = 0; j < voList.size(); j++) {
                    if (voList.get(j).getTime().equals(recordTime)) {
                        voList.get(j).setOutCount(personInOutTrendDto.getCount());
                    }
                }
            }

            List<PersonCarInOutTrendVo> collect =
                    voList.stream().sorted(Comparator.comparing(PersonCarInOutTrendVo::getTime)).collect(Collectors.toList());
            String[] ids = collect.stream().map(PersonCarInOutTrendVo::getTime).toArray(String[]::new);
            Integer[] ins = collect.stream().map(PersonCarInOutTrendVo::getInCount).toArray(Integer[]::new);
            Integer[] outs = collect.stream().map(PersonCarInOutTrendVo::getOutCount).toArray(Integer[]::new);

            resultVo.setTransverseAxis(ids);
            resultVo.setInCountAxis(ins);
            resultVo.setOutCountAxis(outs);

        }
        return Response.ok().wrap(resultVo);
    }

    /**
     * 车辆进出趋势 1本日 2本周 3本月
     * 1进 2出
     *
     * @param scope
     * @param villageId
     * @return
     */
    @GetMapping("carInOutTrend")
    @Operation(summary = "车辆进出趋势折线图 1本日 2本周 3本月")
    public Response<Object> carInOutTrend(
            @Parameter(description = "时间区间类型 1本日 2本周 3本月 必填") @RequestParam(required = false) String scope,
            @Parameter(description = "小区id 非必填") @RequestParam(required = false) Long villageId) {
        if (StringUtils.isEmpty(scope)) {
            return Response.error("缺少查询范围参数或查询范围参数错误");
        }

        Map<String, Object> params = new HashMap<>();
        params.put("scope", scope);
        params.put("villageId", villageId);
        super.fillAuthorityData(params);
        List<PersonCarInOutTrendVo> voList = new ArrayList<>();
        PersonCarInOutTrendVo resultVo = new PersonCarInOutTrendVo();

        // 本日
        if (ScreenConstants.STATISTICS_DAY.equals(scope)) {
            String startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            params.put("startTime", startTime + " 00:00:00");
            params.put("endTime", startTime + " 23:59:59");
            params.put("outOrIn", HouseholdCarConstants.IN);
            List<PersonInOutTrendDto> inTrendDtoList = carRecordService.groupHours(params);
            for (int i = 0; i < inTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = inTrendDtoList.get(i);
                String time = personInOutTrendDto.getTime();
                int count = personInOutTrendDto.getCount();
                PersonCarInOutTrendVo person = new PersonCarInOutTrendVo();
                person.setTime(time);
                person.setInCount(count);
                voList.add(person);
            }

            List<String> hoursList = hoursList();
            for (int i = hoursList.size() - 1; i >= 0; i--) {
                String hourStr = hoursList.get(i);
                for (int k = 0; k < voList.size(); k++) {
                    PersonCarInOutTrendVo dto = voList.get(k);
                    if (hourStr.equals(dto.getTime())) {
                        hoursList.remove(hourStr);
                    }
                }
            }
            for (int i = 0; i < hoursList.size(); i++) {
                PersonCarInOutTrendVo nr = new PersonCarInOutTrendVo();
                nr.setTime(hoursList.get(i));
                nr.setOutCount(0);
                voList.add(nr);
            }

            params.put("outOrIn", HouseholdCarConstants.OUT);
            List<PersonInOutTrendDto> outTrendDtoList = carRecordService.groupHours(params);
            for (int i = 0; i < outTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = outTrendDtoList.get(i);
                String t1 = personInOutTrendDto.getTime();
                for (int j = 0; j < voList.size(); j++) {
                    PersonCarInOutTrendVo person = voList.get(j);
                    String t2 = person.getTime();
                    if (t2.equals(t1)) {
                        person.setOutCount(personInOutTrendDto.getCount());
                    }
                }
            }
            List<PersonCarInOutTrendVo> collect =
                    voList.stream().sorted(Comparator.comparing(PersonCarInOutTrendVo::getTime)).collect(Collectors.toList());
            String[] ids = collect.stream().map(PersonCarInOutTrendVo::getTime).toArray(String[]::new);
            Integer[] ins = collect.stream().map(PersonCarInOutTrendVo::getInCount).toArray(Integer[]::new);
            Integer[] outs = collect.stream().map(PersonCarInOutTrendVo::getOutCount).toArray(Integer[]::new);
            resultVo.setTransverseAxis(ids);
            resultVo.setInCountAxis(ins);
            resultVo.setOutCountAxis(outs);


        }

        // 本周
        if (ScreenConstants.STATISTICS_WEEK.equals(scope)) {
            String[] thisWeeks =
                    DateUtils.timeSlotOfDay(DateUtil.beginOfWeek(new Date()), new Date(), DateUtils.FormatType.DAY, 1);
            params.put("startTime", DateUtil.format(DateUtil.beginOfWeek(new Date()), "yyyy-MM-dd") + " 00:00:00");
            params.put("endTime", DateUtil.format(DateUtil.endOfWeek(new Date()), "yyyy-MM-dd") + " 23:59:59");
            params.put("outOrIn", HouseholdCarConstants.IN);

            for (int i = thisWeeks.length - 1; i >= 0; i--) {
                PersonCarInOutTrendVo record = new PersonCarInOutTrendVo();
                record.setTime(thisWeeks[i]);
                record.setInCount(0);
                record.setOutCount(0);
                voList.add(record);
            }

            List<PersonInOutTrendDto> inTrendDtoList = carRecordService.groupDays(params);
            for (int j = 0; j < inTrendDtoList.size(); j++) {
                PersonInOutTrendDto personInOutTrendDto = inTrendDtoList.get(j);
                String recordTime = personInOutTrendDto.getTime();
                for (int i = 0; i < voList.size(); i++) {
                    if (voList.get(i).getTime().equals(recordTime)) {
                        voList.get(i).setInCount(personInOutTrendDto.getCount());
                    }
                }
            }

            params.put("outOrIn", HouseholdCarConstants.OUT);
            List<PersonInOutTrendDto> outTrendDtoList = carRecordService.groupDays(params);
            for (int i = 0; i < outTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = outTrendDtoList.get(i);
                String recordTime = personInOutTrendDto.getTime();
                for (int j = 0; j < voList.size(); j++) {
                    if (voList.get(j).getTime().equals(recordTime)) {
                        voList.get(j).setOutCount(personInOutTrendDto.getCount());
                    }
                }
            }

            List<PersonCarInOutTrendVo> collect =
                    voList.stream().sorted(Comparator.comparing(PersonCarInOutTrendVo::getTime)).collect(Collectors.toList());
            String[] ids = collect.stream().map(PersonCarInOutTrendVo::getTime).toArray(String[]::new);
            Integer[] ins = collect.stream().map(PersonCarInOutTrendVo::getInCount).toArray(Integer[]::new);
            Integer[] outs = collect.stream().map(PersonCarInOutTrendVo::getOutCount).toArray(Integer[]::new);

            Calendar cal = Calendar.getInstance();
            String yearStr = String.valueOf(cal.get(Calendar.YEAR));
            for (int i = 0; i < ids.length; i++) {
                String format = yearStr + "-" + ids[i];
                String string = DateUtils.dateToWeek(DateUtil.parseDate(format));
                ids[i] = string;
            }
            resultVo.setTransverseAxis(ids);
            resultVo.setInCountAxis(ins);
            resultVo.setOutCountAxis(outs);

        }

        // 本月
        if (ScreenConstants.STATISTICS_MONTH.equals(scope)) {
            String[] thisMonthDays = DateUtils.timeSlotOfDay(DateUtil.beginOfMonth(new Date()), new Date(), DateUtils.FormatType.DAY, 1);
            params.put("startTime", DateUtil.format(DateUtil.beginOfMonth(new Date()), "yyyy-MM-dd") + " 00:00:00");
            params.put("endTime", DateUtil.format(DateUtil.endOfMonth(new Date()), "yyyy-MM-dd") + " 23:59:59");
            params.put("outOrIn", HouseholdCarConstants.IN);

            for (int i = thisMonthDays.length - 1; i >= 0; i--) {
                PersonCarInOutTrendVo record = new PersonCarInOutTrendVo();
                record.setTime(thisMonthDays[i]);
                record.setInCount(0);
                record.setOutCount(0);
                voList.add(record);
            }

            List<PersonInOutTrendDto> inTrendDtoList = carRecordService.groupDays(params);

            for (int j = 0; j < inTrendDtoList.size(); j++) {
                PersonInOutTrendDto personInOutTrendDto = inTrendDtoList.get(j);
                String recordTime = personInOutTrendDto.getTime();
                for (int i = 0; i < voList.size(); i++) {
                    if (voList.get(i).getTime().equals(recordTime)) {
                        voList.get(i).setInCount(personInOutTrendDto.getCount());
                    }
                }
            }

            params.put("outOrIn", HouseholdCarConstants.OUT);
            List<PersonInOutTrendDto> outTrendDtoList = carRecordService.groupDays(params);
            for (int i = 0; i < outTrendDtoList.size(); i++) {
                PersonInOutTrendDto personInOutTrendDto = outTrendDtoList.get(i);
                String recordTime = personInOutTrendDto.getTime();
                for (int j = 0; j < voList.size(); j++) {
                    if (voList.get(j).getTime().equals(recordTime)) {
                        voList.get(j).setOutCount(personInOutTrendDto.getCount());
                    }
                }
            }

            List<PersonCarInOutTrendVo> collect =
                    voList.stream().sorted(Comparator.comparing(PersonCarInOutTrendVo::getTime)).collect(Collectors.toList());
            String[] ids = collect.stream().map(PersonCarInOutTrendVo::getTime).toArray(String[]::new);
            Integer[] ins = collect.stream().map(PersonCarInOutTrendVo::getInCount).toArray(Integer[]::new);
            Integer[] outs = collect.stream().map(PersonCarInOutTrendVo::getOutCount).toArray(Integer[]::new);

            resultVo.setTransverseAxis(ids);
            resultVo.setInCountAxis(ins);
            resultVo.setOutCountAxis(outs);

        }
        return Response.ok().wrap(resultVo);
    }

    /**
     * list 24小时
     *
     * @return
     */
    public static List<String> hoursList() {
        CopyOnWriteArrayList<String> arrayList = new CopyOnWriteArrayList<>();
        arrayList.add("00");
        arrayList.add("01");
        arrayList.add("02");
        arrayList.add("03");
        arrayList.add("04");
        arrayList.add("05");
        arrayList.add("06");
        arrayList.add("07");
        arrayList.add("08");
        arrayList.add("09");
        arrayList.add("10");
        arrayList.add("11");
        arrayList.add("12");
        arrayList.add("13");
        arrayList.add("14");
        arrayList.add("15");
        arrayList.add("16");
        arrayList.add("17");
        arrayList.add("18");
        arrayList.add("19");
        arrayList.add("20");
        arrayList.add("21");
        arrayList.add("22");
        arrayList.add("23");
        return arrayList;
    }
}
